<?php

namespace App\Admin\Controllers;

use App\Admin\Grid\NGrid;
use App\Library\Traits\AdminControllerTrait;
use App\Models\OperateLogModel;
use Encore\Admin\Controllers\AdminController;

class OperateLogController extends AdminController
{
    use AdminControllerTrait;

    protected $title = '操作日志';

    protected function grid(): NGrid
    {
        $grid = new NGrid(new OperateLogModel);
        $grid->model()
            ->select('operate_log.*', 'user_name')
            ->orderByDesc('operate_log.created_at')
            ->leftJoin('shop_user', 'shop_user.user_id', 'operate_log.user_id');
        $grid->column('id', 'ID')->sortable();
        $grid->column('user_id', __('staff.user_name'));
        $grid->column('object_type', '操作类型')->display(function ($column) {
            foreach(__('operate_log') as $k => $v) {
                if ($k == $column) return $v;
            }
        });
        $grid->column('extend', __('l.extend'))->display(function() {
            return view('operate_info', [
                'id' => $this->id,
                'data' => json_encode($this->extend)
            ]);
        });
        $grid->column('created_at', __('l.createdAt'));
        return $grid;
    }
}
